package com.enuo.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils
{
    private static final String timeFormat = "yyyy-MM-dd HH:mm:ss";
    
    private static final String dateFormat1 = "yyyyMMdd";
    
    private static final String dateFormat2 = "yyyy-MM-dd";
    
    private static final String dateFormat3 = "yyyy/MM/dd";
    
    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat();

    public enum DateType
    {
        YEAR, MONTH, DAY
    }
    
    static
    {
        //严格解析
        simpleDateFormat.setLenient(false); 
    }

    /**
     * 得到指定日期格式字符串与当前日期的差值
     * 
     * @param dateStr
     * @return
     */
    public static long deffCurrent(String dateStr)
    {
        simpleDateFormat.applyPattern(timeFormat);
        try
        {
            return simpleDateFormat.parse(dateStr).getTime()
                    - System.currentTimeMillis();
        }
        catch(Exception e)
        {
            return 0l;
        }
    }

    public static Date toDate(String dateString)
    {
        return toDate(dateString, timeFormat);
    }

    public static Date toDate(String dateString, String formart)
    {
        if (null == dateString)
        {
            return null;
        }
        
        simpleDateFormat.applyPattern(formart);
        
        try
        {
            return simpleDateFormat.parse(dateString);
        }
        catch(Exception e)
        {
            return null;
        }
    }
    
    public static Date toDateCompatible(String dateString)
    {
        if (null == dateString)
        {
            return null;
        }
        
        simpleDateFormat.applyPattern(dateFormat1);
        try
        {
            return simpleDateFormat.parse(dateString);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        simpleDateFormat.applyPattern(dateFormat2);
        try
        {
            return simpleDateFormat.parse(dateString);
        }
        catch(Exception e)
        {
        }
        
        simpleDateFormat.applyPattern(dateFormat3);
        try
        {
            return simpleDateFormat.parse(dateString);
        }
        catch(Exception e)
        {
        }
        
        return null;
    }

    public static String f(Date d, String pattern)
    {
        if (null == d)
        {
            return null;
        }

        simpleDateFormat.applyPattern(pattern);
        return simpleDateFormat.format(d);
    }

    public static String f(Date d)
    {
        return f(d, timeFormat);
    }

    /**
     * @Title: getEarliestOfDate
     * @Description: TODO(获取某一天的开始时间00:00:00.000)
     * @param date
     * @return Date 返回类型
     */
    public static Date getEarliestOfDate(Date date)
    {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.HOUR_OF_DAY, 0);
        cDay.set(Calendar.MINUTE, 0);
        cDay.set(Calendar.SECOND, 0);
        cDay.set(Calendar.MILLISECOND, 0);

        return cDay.getTime();
    }

    /**
     * @Title: getLatestOfDate
     * @Description: TODO(获取某一天的结束时间23:59:59.999)
     * @param @param date
     * @param @return 设定文件
     * @return Date 返回类型
     * @throws
     */
    public static Date getLatestOfDate(Date date)
    {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.HOUR_OF_DAY, 23);
        cDay.set(Calendar.MINUTE, 59);
        cDay.set(Calendar.SECOND, 59);
//        cDay.set(Calendar.MILLISECOND, 999);

        return cDay.getTime();
    }

    public static Date getDateByOffset(Date date, int field, int offset)
    {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.add(field, offset);

        return cDay.getTime();
    }

    /**
     * 返回本周周一00:00的date对象
     */
    public static Date getMonday()
    {
        Calendar c = Calendar.getInstance();
        Integer day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
        {
            day_of_week = 7;
        }
        c.add(Calendar.DATE, -day_of_week + 1);
        // 本周周一0点
        Date beginDate = c.getTime();

        return getEarliestOfDate(beginDate);
    }

    public static int getDiffer(Date beginDate, Date endDate, DateType dateType)
    {
        if (null == beginDate || null == endDate || null == dateType)
        {
            return 0;
        }

        if (beginDate.after(endDate))
        {
            Date temp = beginDate;
            beginDate = endDate;
            endDate = temp;
        }

        Calendar beginTime = Calendar.getInstance();
        beginTime.setTime(beginDate);

        Calendar endTime = Calendar.getInstance();
        endTime.setTime(endDate);

        int yearDiffer = endTime.get(Calendar.YEAR)
                - beginTime.get(Calendar.YEAR);

        if (DateType.YEAR.equals(dateType))
        {
            return yearDiffer;
        }

        if (DateType.MONTH.equals(dateType))
        {
            return yearDiffer * 12 + endTime.get(Calendar.MONTH)
                    - beginTime.get(Calendar.MONTH);
        }

        if (DateType.DAY.equals(dateType))
        {
            return (int) ((endTime.getTimeInMillis() - beginTime
                    .getTimeInMillis()) / (1000 * 60 * 60 * 24));
        }

        return 0;
    }
    
    /**
    * @Title: getDayTimestamp 
    * @Description: TODO(获取日时间戳) 
    * @param @param date
    * @param @return
    * @return int    返回类型 
    * @throws
     */
    public static int getDayTimestamp(Date date)
    {
        return (int) (getLatestOfDate(date).getTime() / 24 / 60 / 60 / 1000);
    }

    /**
    * @Title: getTimestamp 
    * @Description: TODO(根据日时间戳获取完整时间戳) 
    * @param @param dayTs
    * @param @return
    * @return long    返回类型 
    * @throws
     */
    public static long getTimestamp(int dayTs)
    {
        return 24l * 60 * 60 * 1000 * dayTs;
    }

    public static void main(String[] args)
    {
        System.out.println(toDateCompatible("2017/7/5"));
    }
    
}